package com.zingrow.web.configure.distribute.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import com.zingrow.common.utils.MongoDBConnection;
import com.zingrow.common.utils.PageView;
import com.zingrow.common.utils.StringUtil;
import com.zingrow.web.configure.distribute.model.InformationDistributeModel;
import com.zingrow.web.configure.distribute.service.IInformationDistributeService;
import com.zingrow.web.customer.mapper.GroupMapper;
import com.zingrow.web.customer.model.Group;
import com.zingrow.web.user.model.User;
import com.zingrow.web.user.service.IUserService;

@Service
public class InformationDistributeService implements IInformationDistributeService {
	MongoDBConnection mongo = MongoDBConnection.getInstance();
	@Autowired
	private IUserService iUserService;
	@Autowired
	private GroupMapper groupMapper;

	// 单个查询
	@Override
	public List<InformationDistributeModel> findifmId(Class<?> InformationDistributeModel, String articleId,
			String ifmId, Integer groupIdS) {
		List<InformationDistributeModel> list = new ArrayList<InformationDistributeModel>();
		Datastore store = mongo.getDatastore();
		Query<?> query = store.createQuery(InformationDistributeModel);
		if (groupIdS != null) {
			// 根据组织ID查询所有方案
			List<Group> glist = groupMapper.selectgroupid(groupIdS);
			List<String> ifmId2 = new ArrayList<String>();
			if (glist.size() > 0) {
				for (Group group : glist) {
					ifmId2.add(group.getIfmIdd());
				}
				query.field("ifmId").in(ifmId2);
				Iterator<?> iterator = query.field("articleId").equal(articleId).iterator();
				while (iterator.hasNext()) {
					InformationDistributeModel bute = (InformationDistributeModel) iterator.next();
					list.add(bute);
				}
			}
		} else {
			Iterator<?> iterator = query.field("articleId").equal(articleId).iterator();
			while (iterator.hasNext()) {
				InformationDistributeModel bute = (InformationDistributeModel) iterator.next();
				list.add(bute);
			}
		}
		return list;
	}

	@Override
	public InformationDistributeModel selectByid(Class<?> InformationDistributeModel, String articleId, String ifmId) {
		Datastore store = mongo.getDatastore();
		InformationDistributeModel bute = new InformationDistributeModel();
		Query<?> query = store.createQuery(InformationDistributeModel);
		query.field("ifmId").equal(ifmId);
		Iterator<?> iterator = query.field("articleId").equal(articleId).iterator();
		while (iterator.hasNext()) {
			bute = (InformationDistributeModel) iterator.next();
		}
		return bute;
	}

	@Override
	public InformationDistributeModel findId(Class<?> InformationDistributeModel, String id) {
		Datastore store = mongo.getDatastore();
		InformationDistributeModel bute = new InformationDistributeModel();
		Query<?> query = store.createQuery(InformationDistributeModel);
		Iterator<?> tor = query.field("_id").equal(id).iterator();
		while (tor.hasNext()) {
			bute = (InformationDistributeModel) tor.next();
		}
		return bute;
	}

	// 查询全部数据
	/*
	 * @Override public List<InformationDistributeModel> findAl(Class<?>
	 * InformationDistributeModel,Date startTime,Date endTime,PageView
	 * pageView){ Datastore store = mongo.getDatastore();
	 * List<InformationDistributeModel> list = new
	 * ArrayList<InformationDistributeModel>(); int pageSize =
	 * pageView.getPageSize(); int pageIndex = pageView.getPageNow(); Query<?>
	 * query = store.createQuery(InformationDistributeModel);
	 * if(startTime!=null&&endTime!=null){
	 * query.and(query.criteria("releaseTime").greaterThan(startTime).criteria(
	 * "releaseTime").lessThan(endTime)); } InformationDistributeModel tion =
	 * new InformationDistributeModel(); AggregationPipeline peline =
	 * store.createAggregation(InformationDistributeModel).match(query).sort(
	 * Sort.descending("sort_timestamp")).skip(pageIndex*pageSize-pageSize).
	 * limit(pageSize); Iterator<?> iterator =
	 * peline.aggregate(InformationDistributeModel);
	 * tion.setCount(query.count()); while(iterator.hasNext()){ tion =
	 * (InformationDistributeModel) iterator.next(); list.add(tion); } return
	 * list; }
	 */
	// 多条件模糊查询
	@Override
	public List<InformationDistributeModel> findGroup(Class<?> InformationDistributeModel, String title, String content,
			String websiteName, Date startTime, Date endTime, String articleType, PageView pageView,
			List<String> idlist, String url) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("title", title);
		map.put("content", content);
		map.put("websiteName", websiteName);
		if (startTime != null) {
			map.put("startTime", startTime);
		}
		if (endTime != null) {
			map.put("endTime", endTime);
		}
		map.put("articleType", articleType);
		if (null != idlist && !idlist.isEmpty()) {
			map.put("idList", idlist);
		}
		map.put("url", url);
		List<InformationDistributeModel> list = mongo.findGroup(InformationDistributeModel, map, pageView);
		if (list.size()>0) {
			for (InformationDistributeModel model : list) {
				// 判断类型
				if (model.getArticleType().equals("Micro")) {
					if (StringUtils.isNotBlank(model.getWeiboContent())||StringUtils.isNotBlank(model.getWeiboSourceContent())) {
						//微博正文
						model.setWeiboContent(model.getWeiboContent().replaceAll("　| |　", "").replaceAll("</?[^>]+>", ""));
						//model.setWeiboContent(model.getWeiboContent().replaceAll("\\&[a-zA-Z]{1,10};", ""));
						model.setWeiboContent(model.getWeiboContent().replaceAll("</?[^>]+>", "").trim());
						//微博转发文
						model.setWeiboSourceContent(model.getWeiboSourceContent().replaceAll("　| |　", "").replaceAll("</?[^>]+>", ""));
						//model.setWeiboSourceContent(model.getWeiboSourceContent().replaceAll("</?[^>]+>", "").trim());
						//定义一些特殊字符的正则表达式 如：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						model.setWeiboSourceContent(model.getWeiboSourceContent().replaceAll("\\&[a-zA-Z]{1,10};", ""));
						//微博正文加转发
						model.setContent(model.getWeiboContent()+model.getWeiboSourceContent());
						//model.setContent(HtmlUtils.htmlEscape(model.getWeiboContent()+model.getWeiboSourceContent()));
					} 
				} else {
					model.setContent(model.getContent().replaceAll(" | |　", "").replaceAll("</?[^>]+>", ""));
					//model.setContent(model.getContent().replaceAll("</?[^>]+>", "").trim());
					// html转译
					model.setContent(HtmlUtils.htmlEscape(model.getContent()));
					model.setContent(model.getContent().replaceAll("\\&[a-zA-Z]{1,10};", ""));
				}
			}
		}
		return list;
	}

	// 查询相似数据根据多个条件和groupID
	/*
	 * public List<InformationDistributeModel> findGroupId(Class<?>
	 * InformationDistributeModel, String title, String content, String
	 * websiteName, Date startTime, Date endTime, String articleType, PageView
	 * pageView, List<String> idlist, String url,String groupId){
	 * 
	 * }
	 */
	// 修改
	@Override
	public boolean update(InformationDistributeModel bute) {
		return mongo.Update(bute);
	}

	// 删除方案与数据的关系
	@Override
	public boolean delete(String articleId, String ifmId) {
		return mongo.delete(InformationDistributeModel.class, articleId, ifmId);
	}

	// 删除
	@Override
	public boolean deleteByField(Class<?> InformationDistributeModel, List<String> groupId, Date startTime, Date endTime,
			String idlist) {
		Map<String, Object> map = new HashMap<String, Object>();
		//map.put("groupId", groupId);
		if (startTime != null) {
			map.put("startTime", startTime);
		}
		if (endTime != null) {
			map.put("endTime", endTime);
		}
		if (StringUtils.isNotBlank(idlist)) {
			map.put("idList", idlist);
		}
		return mongo.deleteByField(InformationDistributeModel,map,groupId);
	}
	

	/*
	 * //查询有多少条数据
	 * 
	 * @Override public Integer selectcount(Class<?>
	 * InformationDistributeModel){ return
	 * mongo.selectcount(InformationDistributeModel); }
	 */
	// 导出列表
	/*
	 * @Override public List<InformationDistributeModel> findAllid( Class<?>
	 * InformationDistributeModel, List<String> ids) { // 根据id集合查询数据
	 * List<InformationDistributeModel> list = mongo.findAllid(
	 * InformationDistributeModel, ids); List<InformationDistributeModel>
	 * buteList = new ArrayList<InformationDistributeModel>();
	 * List<InformationDistributeModel> modelList = new
	 * ArrayList<InformationDistributeModel>(); for (InformationDistributeModel
	 * distribute : list) { // 根据方案id查询用户名称 List<User> listuser =
	 * iUserService.selectidName(distribute .getIfmId()); // 根据groupId查询相似
	 * buteList = mongo.findlike(InformationDistributeModel,
	 * distribute.getGroupId()); if (buteList.size() > 1) { for
	 * (InformationDistributeModel bute : buteList) {
	 * buteList.get(0).setGroupId("原创"); // 如果是原创就显示相似数 if
	 * (bute.getGroupId().equals("原创")) { buteList.get(0).setReprintNumb(
	 * Integer.toString(buteList.size())); } else { bute.setReprintNumb("");
	 * bute.setGroupId(""); } if (bute.getAuthor() == null ||
	 * bute.getAuthor().equals("")) { bute.setAuthor("无"); } if
	 * (StringUtils.isNotBlank(bute.getIfmId())) { listuser =
	 * iUserService.selectidName(bute.getIfmId()); for (User user : listuser) {
	 * // 把用户名称加进对象 bute.setUserName(user.getUserName()); } } if
	 * (StringUtils.isNotBlank(bute.getArticleType())) { if
	 * (bute.getArticleType().equals("BBS")) { // 如果信息类型是微信和论坛，就显示阅读量 //
	 * bute.setReadNumb(bute.getReadNumb());//阅读量 //
	 * bute.setReplyNumb(bute.getReplyNumb());//回复数 bute.setPointNum(0);// 点赞量 }
	 * if (bute.getArticleType().equals("WeiXin")) { // 如果信息类型是微信，就显示点赞量 //
	 * bute.setReadNumb(bute.getReadNumb());//阅读量 //
	 * bute.setPointNum(bute.getPointNum());//点赞量 bute.setReplyNumb(0);// 回复数 }
	 * } if (bute.getArticleType().equals("Micro")) { if
	 * (StringUtils.isNotBlank(bute.getWeiboContent())) {
	 * bute.setContent(Integer.toString(bute.getWeiboContent().length())); }
	 * else if (StringUtils.isNotBlank(bute.getWeiboSourceContent())) {
	 * bute.setContent(Integer.toString(bute.getWeiboSourceContent().length()));
	 * } } if (StringUtils.isNotBlank(bute.getContent()) &&
	 * !bute.getArticleType().equals("Micro")) { // 统计正文字数
	 * bute.setContent(Integer.toString(bute.getContent().length())); } if
	 * (bute.getArticleType().equals("Micro")) { if (bute.getWeiboBlogger() ==
	 * null || bute.getWeiboBlogger().equals("")) { bute.setWebsiteName("无"); }
	 * else { bute.setWebsiteName(bute.getWeiboBlogger()); } }
	 * modelList.add(bute); } } else { if
	 * (StringUtils.isNotBlank(distribute.getArticleType())) { if
	 * (distribute.getArticleType().equals("BBS")) { // 如果信息类型是微信和论坛，就显示阅读量 //
	 * bute.setReadNumb(bute.getReadNumb());//阅读量 //
	 * bute.setReplyNumb(bute.getReplyNumb());//回复数 distribute.setPointNum(0);//
	 * 点赞量 } if (distribute.getArticleType().equals("WeiXin")) { //
	 * 如果信息类型是微信，就显示点赞量 // bute.setReadNumb(bute.getReadNumb());//阅读量 //
	 * bute.setPointNum(bute.getPointNum());//点赞量 distribute.setReplyNumb(0);//
	 * 回复数 } } for (User user : listuser) { // 把用户名称加进对象
	 * distribute.setUserName(user.getUserName()); }
	 * distribute.setGroupId("原创"); // 如果是原创就显示相似数 if
	 * (distribute.getGroupId().equals("原创")) { distribute.setReprintNumb(""); }
	 * if (distribute.getAuthor() == null || distribute.getAuthor().equals(""))
	 * { distribute.setAuthor("无"); } if
	 * (distribute.getArticleType().equals("Micro")) { if
	 * (StringUtils.isNotBlank(distribute.getWeiboContent())) {
	 * distribute.setContent(Integer.toString(distribute.getWeiboContent().
	 * length())); } else if
	 * (StringUtils.isNotBlank(distribute.getWeiboSourceContent())) {
	 * distribute.setContent(Integer.toString(distribute.getWeiboSourceContent()
	 * .length())); } } if (StringUtils.isNotBlank(distribute.getContent())&&
	 * !distribute.getArticleType().equals("Micro")) { // 统计正文字数
	 * distribute.setContent(Integer.toString(distribute.getContent().length()))
	 * ; } if (distribute.getArticleType().equals("Micro")) { if
	 * (distribute.getWeiboBlogger() == null ||
	 * "".equals(distribute.getWeiboBlogger())) {
	 * distribute.setWeiboBlogger("无"); } else {
	 * distribute.setWebsiteName(distribute.getWeiboBlogger()); } }
	 * modelList.add(distribute); } } return modelList; }
	 */

	// 根据条件查询--导出所有数据列表
	@Override
	public List<InformationDistributeModel> findGroupAll(Class<?> InformationDistributeModel, String title,
			String content, String websiteName, Date startTime, Date endTime, String articleType, String url,
			List<String> idlist) {
		List<InformationDistributeModel> list = new ArrayList<InformationDistributeModel>();
		List<InformationDistributeModel> buteList = new ArrayList<InformationDistributeModel>();
		List<InformationDistributeModel> modelList = new ArrayList<InformationDistributeModel>();
		List<User> userlist = new ArrayList<User>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("title", title);
		map.put("content", content);
		map.put("websiteName", websiteName);
		if (startTime != null) {
			map.put("startTime", startTime);
		}
		if (endTime != null) {
			map.put("endTime", endTime);
		}
		map.put("articleType", articleType);
		if (null != idlist && !idlist.isEmpty()) {
			map.put("idList", idlist);
		}
		map.put("url", url);
		list = mongo.findGroupAll(InformationDistributeModel, map);
		for (InformationDistributeModel distribute : list) {
			// 根据方案id查询用户名称
			userlist = iUserService.selectidName(distribute.getIfmId());
			// 根据groupId查询相似
			buteList = mongo.findlike(InformationDistributeModel, distribute.getGroupId(), map);
			if (buteList.size() > 1) {
				for (InformationDistributeModel bute : buteList) {
					buteList.get(0).setGroupId("原创");
					// 如果是原创就显示相似数
					if (bute.getGroupId().equals("原创")) {
						buteList.get(0).setReprintNumb(Integer.toString(buteList.size()));
					} else {
						bute.setReprintNumb("");
						bute.setGroupId("");
					}
					if (bute.getAuthor() == null || bute.getAuthor().equals("")) {
						bute.setAuthor("无");
					}
					if (StringUtils.isNotBlank(bute.getIfmId())) {
						userlist = iUserService.selectidName(bute.getIfmId());
						for (User user : userlist) {
							// 把用户名称加进对象
							bute.setUserName(user.getUserName());
						}
					}
					if (StringUtils.isNotBlank(bute.getArticleType())) {
						if (bute.getArticleType().equals("BBS")) {
							// 如果信息类型是微信和论坛，就显示阅读量
							// bute.setReadNumb(bute.getReadNumb());//阅读量
							// bute.setReplyNumb(bute.getReplyNumb());//回复数
							bute.setPointNum(0);// 点赞量
						}
						if (bute.getArticleType().equals("WeiXin")) {
							// 如果信息类型是微信，就显示点赞量
							// bute.setReadNumb(bute.getReadNumb());//阅读量
							// bute.setPointNum(bute.getPointNum());//点赞量
							bute.setReplyNumb(0);// 回复数
						}
					}
					if (bute.getArticleType().equals("Micro")) {
						if (StringUtils.isNotBlank(bute.getWeiboContent())) {
							bute.setContent(Integer.toString(bute.getWeiboContent().length()));
						} else if (StringUtils.isNotBlank(bute.getWeiboSourceContent())) {
							bute.setContent(Integer.toString(bute.getWeiboSourceContent().length()));
						}
					}
					if (StringUtils.isNotBlank(bute.getContent()) && !bute.getArticleType().equals("Micro")) {
						// 统计正文字数
						bute.setContent(Integer.toString(bute.getContent().length()));
					}
					if (bute.getArticleType().equals("Micro")) {
						if (bute.getWeiboBlogger() == null || "".equals(bute.getWeiboBlogger())) {
							bute.setWebsiteName("无");
						} else {
							bute.setWebsiteName(bute.getWeiboBlogger());
						}
					}
					modelList.add(bute);
				}
			} else {
				if (StringUtils.isNotBlank(distribute.getArticleType())) {
					if (distribute.getArticleType().equals("BBS")) {
						// 如果信息类型是微信和论坛，就显示阅读量
						// bute.setReadNumb(bute.getReadNumb());//阅读量
						// bute.setReplyNumb(bute.getReplyNumb());//回复数
						distribute.setPointNum(0);// 点赞量
					}
					if (distribute.getArticleType().equals("WeiXin")) {
						// 如果信息类型是微信，就显示点赞量
						// bute.setReadNumb(bute.getReadNumb());//阅读量
						// bute.setPointNum(bute.getPointNum());//点赞量
						distribute.setReplyNumb(0);// 回复数
					}
				}
				for (User user : userlist) {
					// 把用户名称加进对象
					distribute.setUserName(user.getUserName());
				}
				distribute.setGroupId("原创");
				// 如果是原创就显示相似数
				if (distribute.getGroupId().equals("原创")) {
					distribute.setReprintNumb("");
				}
				if (distribute.getAuthor() == null || distribute.getAuthor().equals("")) {
					distribute.setAuthor("无");
				}
				if (distribute.getArticleType().equals("Micro")) {
					if (StringUtils.isNotBlank(distribute.getWeiboContent())) {
						distribute.setContent(Integer.toString(distribute.getWeiboContent().length()));
					} else if (StringUtils.isNotBlank(distribute.getWeiboSourceContent())) {
						distribute.setContent(Integer.toString(distribute.getWeiboSourceContent().length()));
					}
				}
				if (StringUtils.isNotBlank(distribute.getContent()) && !distribute.getArticleType().equals("Micro")) {
					// 统计正文字数
					distribute.setContent(Integer.toString(distribute.getContent().length()));
				}
				if (distribute.getArticleType().equals("Micro")) {
					if (distribute.getWeiboBlogger() == null || "".equals(distribute.getWeiboBlogger())) {
						distribute.setWebsiteName("无");
					} else {
						distribute.setWebsiteName(distribute.getWeiboBlogger());
					}
				}

				modelList.add(distribute);
			}
		}
		return modelList;
	}

	// 导出word文档
	@Override
	public List<InformationDistributeModel> exportWordForPageData(Class<?> InformationDistributeModel, List<String> ids,
			String title, String content, String websiteName, Date startTime, Date endTime, String articleType,
			String url, List<String> idlist) {
		List<InformationDistributeModel> list = new ArrayList<InformationDistributeModel>();
		List<InformationDistributeModel> buteList = new ArrayList<InformationDistributeModel>();
		List<InformationDistributeModel> modelList = new ArrayList<InformationDistributeModel>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("title", title);
		map.put("content", content);
		map.put("websiteName", websiteName);
		if (startTime != null) {
			map.put("startTime", startTime);
		}
		if (endTime != null) {
			map.put("endTime", endTime);
		}
		map.put("articleType", articleType);
		if (null != ids && !ids.isEmpty()) {
			map.put("ids", ids);
		}
		map.put("url", url);
		// list集合存放根据新闻唯一id查询的数据
		list = mongo.findwordId(InformationDistributeModel, idlist);
		for (InformationDistributeModel butelist : list) {
			// buteList集合存放根据groupId查询的相似数据
			buteList = mongo.findlike(InformationDistributeModel, butelist.getGroupId(), map);
			if (buteList.size() > 1) {
				for (InformationDistributeModel model : buteList) {
					// 标题 判断类型
					if (model.getArticleType().equals("Micro")) {
						model.setTitle("微博");
					} else {
						if (model.getTitle() == null || model.getTitle().equals("")) {
							model.setTitle("无");
						}
					}
					// 判断首发媒体
					if (model.getArticleType().equals("Micro")) {
						if (StringUtils.isNotBlank(model.getWeiboBlogger())) {
							model.setWebsiteName(model.getWeiboBlogger());
						} else {
							model.setWebsiteName("无");
						}
					} else {
						if (model.getWebsiteName() == null || model.getWebsiteName().equals("")) {
							model.setWebsiteName("无");
						}
					}
					// 判断作者
					if (model.getAuthor() == null || model.getAuthor().equals("")) {
						model.setAuthor("无");
					}
					// 判断内容
					if (model.getArticleType().equals("Micro")) {
						// 去标签
						if (StringUtils.isNotBlank(model.getWeiboSourceContent()) || StringUtils.isNotBlank(model.getWeiboContent())) {
							model.setContent(Jsoup.parse(model.getWeiboSourceContent() + model.getWeiboContent()).text());
							model.setContent(model.getContent().replaceAll("&", "&amp;")
									.replaceAll("<", "&lt;").replaceAll(">", "&gt;"));
						} /*else if (StringUtils.isNotBlank(model.getWeiboContent())) {
							model.setContent(Jsoup.parse(model.getWeiboContent()).text());
							model.setContent(model.getWeiboContent().replaceAll("&", "&amp;")
									.replaceAll("<", "&lt;").replaceAll(">", "&gt;"));
							model.setContent(model.getWeiboContent().replaceAll("　| ", "").replaceAll("<|>", "")
									.replaceAll("\\&[a-zA-Z]{1,10};", ""));
						} */else {
							model.setContent("无");
						}
					} else {
						if (model.getContent() == null || "".equals(model.getContent())) {
							model.setContent("无");
						}
						model.setContent(Jsoup.parse(model.getContent()).text());
						model.setContent(model.getContent().replaceAll("&", "&amp;")
								.replaceAll("<", "&lt;").replaceAll(">", "&gt;"));
						/*model.setContent(model.getContent().replaceAll("　| ", "").replaceAll("<|>", "")
								.replaceAll("\\&[a-zA-Z]{1,10};", ""));*/
					}
					modelList.add(model);
				}
			} else {
				buteList = mongo.findlike(InformationDistributeModel, butelist.getGroupId(), map);
				for (InformationDistributeModel bute : buteList) {
					// 标题 判断类型
					if (bute.getArticleType().equals("Micro")) {
						bute.setTitle("微博");
					} else {
						if (bute.getTitle() == null || "".equals(bute.getTitle())) {
							bute.setTitle("无");
						}
					}
					// 判断首发媒体
					if (bute.getArticleType().equals("Micro")) {
						if (StringUtils.isNotBlank(bute.getWeiboBlogger())) {
							bute.setWebsiteName(bute.getWeiboBlogger());
						} else {
							bute.setWebsiteName("无");
						}
					} else {
						if (bute.getWebsiteName() == null || bute.getWebsiteName().equals("")) {
							bute.setWebsiteName("无");
						}
					}
					// 判断作者
					if (bute.getAuthor() == null || bute.getAuthor().equals("")) {
						bute.setAuthor("无");
					}
					// 判断内容
					if (bute.getArticleType().equals("Micro")) {
						// 去标签
						if (StringUtils.isNotBlank(bute.getWeiboSourceContent()) || StringUtils.isNotBlank(bute.getWeiboContent())) {
							bute.setContent(Jsoup.parse(bute.getWeiboSourceContent() + bute.getWeiboContent()).text());
							/*bute.setContent(bute.getWeiboSourceContent().replaceAll("　| ", "")
									.replaceAll("<|>", "").replaceAll("\\&[a-zA-Z]{1,10};", ""));*/
							bute.setContent(bute.getContent().replaceAll("&", "&amp;")
									.replaceAll("<", "&lt;").replaceAll(">", "&gt;"));
						} /*else if (StringUtils.isNotBlank(bute.getWeiboContent())) {
							bute.setContent(bute.getWeiboContent().replaceAll("　| ", "").replaceAll("<|>", "")
									.replaceAll("\\&[a-zA-Z]{1,10};", ""));
							bute.setContent(Jsoup.parse(bute.getWeiboContent()).text());
							bute.setContent(bute.getWeiboContent().replaceAll("&", "&amp;")
									.replaceAll("<", "&lt;").replaceAll(">", "&gt;"));
						} */else {
							bute.setContent("无");
						}
					} else {
						if (bute.getContent() == null || bute.getContent().equals("")) {
							bute.setContent("无");
						}
						//bute.setContent(HtmlUtils.htmlEscape(bute.getContent()));
						bute.setContent(Jsoup.parse(bute.getContent()).text());
						bute.setContent(bute.getContent().replaceAll("&", "&amp;")
								.replaceAll("<", "&lt;").replaceAll(">", "&gt;"));
						//bute.setContent(bute.getContent().replaceAll("　| ", "").replaceAll("<|>", "").replaceAll("\\&[a-zA-Z]{1,10};", ""));
					}
					modelList.add(bute);
				}
			}
		}
		return modelList;
	}

	// 根据条件查询--导出所有文章
	public List<InformationDistributeModel> findWordAll(Class<?> InformationDistributeModel, String title,
			String content, String websiteName, Date startTime, Date endTime, String articleType, String url,
			List<String> idlist) {
		List<InformationDistributeModel> list = new ArrayList<InformationDistributeModel>();
		List<InformationDistributeModel> buteList = new ArrayList<InformationDistributeModel>();
		List<InformationDistributeModel> modelList = new ArrayList<InformationDistributeModel>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("title", title);
		map.put("content", content);
		map.put("websiteName", websiteName);
		if (startTime != null) {
			map.put("startTime", startTime);
		}
		if (endTime != null) {
			map.put("endTime", endTime);
		}
		map.put("articleType", articleType);
		if (null != idlist && !idlist.isEmpty()) {
			map.put("idList", idlist);
		}
		map.put("url", url);
		list = mongo.findGroupAll(InformationDistributeModel, map);
		for (InformationDistributeModel butelist : list) {
			// buteList集合存放根据groupId查询的相似数据
			buteList = mongo.findlike(InformationDistributeModel, butelist.getGroupId(), map);
			if (buteList.size() > 1) {
				for (InformationDistributeModel model : buteList) {
					// 标题 判断类型
					if (model.getArticleType().equals("Micro")) {
						model.setTitle("微博");
					} else {
						if (model.getTitle() == null || "".equals(model.getTitle())) {
							model.setTitle("无");
						}
					}
					// 判断首发媒体
					if (model.getArticleType().equals("Micro")) {
						if (StringUtils.isNotBlank(model.getWeiboBlogger())) {
							model.setWebsiteName(model.getWeiboBlogger());
						} else {
							model.setWebsiteName("无");
						}
					} else {
						if (model.getWebsiteName() == null || model.getWebsiteName().equals("")) {
							model.setWebsiteName("无");
						}
					}
					// 判断作者
					if (model.getAuthor() == null || model.getAuthor().equals("")) {
						model.setAuthor("无");
					}
					// 判断内容
					if (model.getArticleType().equals("Micro")) {
						// 去标签
						if (StringUtils.isNotBlank(model.getWeiboSourceContent())) {
							model.setContent(model.getWeiboSourceContent().replaceAll("　| ", "")
									.replaceAll("</?[^>]+>", "").replaceAll("&nbsp;", ""));
						} else if (StringUtils.isNotBlank(model.getWeiboContent())) {
							model.setContent(model.getWeiboContent().replaceAll("　| ", "").replaceAll("</?[^>]+>", "")
									.replaceAll("&nbsp;", ""));
						} else {
							model.setContent("无");
						}
					} else {
						if (model.getContent() == null || model.getContent().equals("")) {
							model.setContent("无");
						}
						model.setContent(model.getContent().replaceAll("　| ", "").replaceAll("</?[^>]+>", "")
								.replaceAll("&nbsp;", ""));
					}
					modelList.add(model);
				}
			} else {
				for (InformationDistributeModel bute : list) {
					// 标题 判断类型
					if (bute.getArticleType().equals("Micro")) {
						bute.setTitle("微博");
					} else {
						if (bute.getTitle() == null || "".equals(bute.getTitle())) {
							bute.setTitle("无");
						}
					}
					// 判断首发媒体
					if (bute.getArticleType().equals("Micro")) {
						if (StringUtils.isNotBlank(bute.getWeiboBlogger())) {
							bute.setWebsiteName(bute.getWeiboBlogger());
						} else {
							bute.setWebsiteName("无");
						}
					} else {
						if (bute.getWebsiteName() == null || bute.getWebsiteName().equals("")) {
							bute.setWebsiteName("无");
						}
					}
					// 判断作者
					if (bute.getAuthor() == null || bute.getAuthor().equals("")) {
						bute.setAuthor("无");
					}
					// 判断内容
					if (bute.getArticleType().equals("Micro")) {
						// 去标签
						if (StringUtils.isNotBlank(bute.getWeiboSourceContent())) {
							bute.setContent(bute.getWeiboSourceContent().replaceAll("　| ", "")
									.replaceAll("</?[^>]+>", "").replaceAll("&nbsp;", ""));
						} else if (StringUtils.isNotBlank(bute.getWeiboContent())) {
							bute.setContent(bute.getWeiboContent().replaceAll("　| ", "").replaceAll("</?[^>]+>", "")
									.replaceAll("&nbsp;", ""));
						} else {
							bute.setContent("无");
						}
					} else {
						if (bute.getContent() == null || bute.getContent().equals("")) {
							bute.setContent("无");
						}
						bute.setContent(bute.getContent().replaceAll("　| ", "").replaceAll("</?[^>]+>", "")
								.replaceAll("&nbsp;", ""));
					}
					modelList.add(bute);
				}
			}
		}
		return modelList;
	}

	@Override
	public List<InformationDistributeModel> findAllid(Class<?> InformationDistributeModel, List<String> ids,
			String title, String content, String websiteName, Date startTime, Date endTime, String articleType,
			String url, List<String> idlist) {
		// 根据id集合查询数据
		List<InformationDistributeModel> list = mongo.findAllid(InformationDistributeModel, idlist);
		List<InformationDistributeModel> buteList = new ArrayList<InformationDistributeModel>();
		List<InformationDistributeModel> modelList = new ArrayList<InformationDistributeModel>();
		Map<String, Object> map = new HashMap<String, Object>();
		if (ids.size() > 0) {
			map.put("ids", ids);
		}
		map.put("title", title);
		map.put("content", content);
		map.put("websiteName", websiteName);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("articleType", articleType);
		map.put("url", url);
		for (InformationDistributeModel distribute : list) {
			// 根据方案id查询用户名称
			List<User> listuser = iUserService.selectidName(distribute.getIfmId());
			// 根据groupId查询相似
			buteList = mongo.findlike(InformationDistributeModel, distribute.getGroupId(), map);
			if (buteList.size() > 1) {
				buteList.get(0).setReprintNumb(Integer.toString(buteList.size()));
				for (InformationDistributeModel bute : buteList) {
					if (bute.isOriginal()==true) {
						bute.setGroupId("原创");
					}
					// 如果是原创就显示相似数
					if (!bute.getGroupId().equals("原创")) {
						bute.setGroupId("");
						//buteList.get(0).setReprintNumb(Integer.toString(buteList.size()));
					}
					if (bute.getAuthor() == null || bute.getAuthor().equals("")) {
						bute.setAuthor("无");
					}
					List<String> str = new ArrayList<String>();
					if (StringUtils.isNotBlank(bute.getIfmId())) {
						listuser = iUserService.selectidName(bute.getIfmId());
						for (User user : listuser) {
							str.add(user.getUserName());
							String name = StringUtil.toStringjoin(str);
							// 把用户名称加进对象
							bute.setUserName(name);
						}
					}
					if (StringUtils.isNotBlank(bute.getArticleType())) {
						if (bute.getArticleType().equals("BBS")) {
							// 如果信息类型是微信和论坛，就显示阅读量
							// bute.setReadNumb(bute.getReadNumb());//阅读量
							// bute.setReplyNumb(bute.getReplyNumb());//回复数
							bute.setPointNum(0);// 点赞量
						}
						if (bute.getArticleType().equals("WeiXin")) {
							// 如果信息类型是微信，就显示点赞量
							// bute.setReadNumb(bute.getReadNumb());//阅读量
							// bute.setPointNum(bute.getPointNum());//点赞量
							bute.setReplyNumb(0);// 回复数
						}
					}
					if (bute.getArticleType().equals("Micro")) {
						if (StringUtils.isNotBlank(bute.getWeiboContent())) {
							bute.setContent(Integer.toString(bute.getWeiboContent().length()));
						} else if (StringUtils.isNotBlank(bute.getWeiboSourceContent())) {
							bute.setContent(Integer.toString(bute.getWeiboSourceContent().length()));
						}
					}
					if (StringUtils.isNotBlank(bute.getContent()) && !bute.getArticleType().equals("Micro")) {
						// 统计正文字数
						bute.setContent(Integer.toString(bute.getContent().length()));
					}
					if (bute.getArticleType().equals("Micro")) {
						if (bute.getWeiboBlogger() == null || bute.getWeiboBlogger().equals("")) {
							bute.setWebsiteName("无");
						} else {
							bute.setWebsiteName(bute.getWeiboBlogger());
						}
					}
					modelList.add(bute);
				}
			} else {
				if (StringUtils.isNotBlank(distribute.getArticleType())) {
					if (distribute.getArticleType().equals("BBS")) {
						// 如果信息类型是微信和论坛，就显示阅读量
						// bute.setReadNumb(bute.getReadNumb());//阅读量
						// bute.setReplyNumb(bute.getReplyNumb());//回复数
						distribute.setPointNum(0);// 点赞量
					}
					if (distribute.getArticleType().equals("WeiXin")) {
						// 如果信息类型是微信，就显示点赞量
						// bute.setReadNumb(bute.getReadNumb());//阅读量
						// bute.setPointNum(bute.getPointNum());//点赞量
						distribute.setReplyNumb(0);// 回复数
					}
				}
				List<String> str = new ArrayList<String>();
				for (User user : listuser) {
					str.add(user.getUserName());
					String name = StringUtil.toStringjoin(str);
					// 把用户名称加进对象
					distribute.setUserName(name);
				}
				distribute.setGroupId("原创");
				// 如果是原创就显示相似数
				if (distribute.getGroupId().equals("原创")) {
					distribute.setReprintNumb("");
				}
				if (distribute.getAuthor() == null || distribute.getAuthor().equals("")) {
					distribute.setAuthor("无");
				}
				if (distribute.getArticleType().equals("Micro")) {
					if (StringUtils.isNotBlank(distribute.getWeiboContent())) {
						distribute.setContent(Integer.toString(distribute.getWeiboContent().length()));
					} else if (StringUtils.isNotBlank(distribute.getWeiboSourceContent())) {
						distribute.setContent(Integer.toString(distribute.getWeiboSourceContent().length()));
					}
				}
				if (StringUtils.isNotBlank(distribute.getContent()) && !distribute.getArticleType().equals("Micro")) {
					// 统计正文字数
					distribute.setContent(Integer.toString(distribute.getContent().length()));
				}
				if (distribute.getArticleType().equals("Micro")) {
					if (distribute.getWeiboBlogger() == null || "".equals(distribute.getWeiboBlogger())) {
						distribute.setWeiboBlogger("无");
					} else {
						distribute.setWebsiteName(distribute.getWeiboBlogger());
					}
				}
				modelList.add(distribute);
			}
		}
		return modelList;
	}

}
